import bpy





import 物体.物体管理.物体数据 as 网格数据

from 节点.全局控制.Node.节点和插座基类 import bl_节点基类 
from 物体.物体管理 import engine_物体创建
from 节点.Core import bl_节点构建模板
from 节点.Core import bl_UI配色

import engine.initLIB.load_dll as engineLib
import ctypes




engineLib.flexCAPI.f_创建物理形状几何体节点.restype = ctypes.c_bool

engineLib.flexCAPI.f_创建物理体添加到引擎节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建物理体节点.restype = ctypes.c_bool






E_物理引擎类型枚举 = [
	("TYPE_PhysX", "PhysX", "", 0),
	("TYPE_FleX", "FleX", "", 1),
	("TYPE_ODE", "ODE", "", 2),
	("TYPE_NewTon", "NewTon", "", 3),
	("TYPE_Bullet", "Bullet", "", 4),
	("TYPE_Chrono", "Chrono", "", 5),
]


E_物理体类型枚举 = [
	("TYPE_刚体", "刚体", "", 0),
	("TYPE_柔体", "柔体", "", 1),
	("TYPE_布料", "布料", "", 2),
	("TYPE_静态", "静态", "", 3),
	("TYPE_控制", "控制", "", 4),
]



E_物理形状几何体类型枚举 = [
	("TYPE_球",		"球",		"", 'MESH_UVSPHERE',	0),
	("TYPE_胶囊",	"胶囊",		"", 'MESH_CAPSULE',		1),
	("TYPE_平面",	"平面",		"", 'MESH_PLANE',		2),
	("TYPE_立方体",	"立方体",	"", 'MESH_CUBE',		3),
	("TYPE_凸壳",	"凸壳",		"", 'MESH_ICOSPHERE',	4),
	("TYPE_网格",	"网格",		"", 'MOD_TRIANGULATE',	5),
	("TYPE_地形",	"地形",		"", 'MOD_MULTIRES',		6),
]







E_物理材质预设类型枚举 = [
	("TYPE_默认",		"默认",		"", 	0),
]



def f_update_材质预设(self, context):
	data = list(str(self.m_材质预设))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])


class C_Node_创建物理材质(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_创建物理材质'
	bl_label = '创建物理材质'
	
	m_材质预设 = bpy.props.EnumProperty(items=E_物理材质预设类型枚举, name="几何体", default="TYPE_默认", update=f_update_材质预设)
	def init(self, context):
		super(C_Node_创建物理材质, self).f_init(context)
		self.inputs.new('C_Socket_I32', name='物理场景ID')
		self.inputs.new('C_Socket_F32_Array1D', name='静摩擦')
		self.inputs.new('C_Socket_F32_Array1D', name='动摩擦')
		self.inputs.new('C_Socket_F32_Array1D', name='恢复系数')
		
		self.outputs.new('C_Socket_物理材质_Array1D', name='物理体')
		
		self.f_toCPP(self.id_data);

	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_材质预设')
		
	def f_toCPP(self, tree):
		头次创建 = engineLib.flexCAPI.f_创建物理材质节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 头次创建 :
			self.use_custom_color = True
			self.color = bl_UI配色.g_物理节点颜色
			#self.

class C_NODE_MENU_创建物理材质(bpy.types.Operator):
	bl_idname = 'node.global_node_obj_add_physics_material'
	bl_label = '创建物理材质'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_创建物理材质', use_transform=True)
		return {'FINISHED'}













def f_update_物理引擎类型(self, context):
	data = list(str(self.m_物理引擎类型))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])


def f_update_物理形状几何体类型(self, context):
	data = list(str(self.m_物理形状几何体类型))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])


class C_Node_创建物理体形状(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_创建物理体形状'
	bl_label = '创建物理体形状'
	
	m_物理引擎类型 = bpy.props.EnumProperty(items=E_物理引擎类型枚举, name="引擎", default="TYPE_Chrono", update=f_update_物理引擎类型)
	m_物理形状几何体类型 = bpy.props.EnumProperty(items=E_物理形状几何体类型枚举, name="几何体", default="TYPE_球", update=f_update_物理形状几何体类型)
	def init(self, context):
		super(C_Node_创建物理体形状, self).f_init(context)
		self.inputs.new('C_Socket_I32', name='物理场景ID')
		self.inputs.new('C_Socket_引用', name='几何参数')
		
		self.outputs.new('C_Socket_物理几何形状_Array1D', name='几何体')
		
		self.f_toCPP(self.id_data);

	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_物理引擎类型')
		layout.prop(self, 'm_物理形状几何体类型')
		
		
	def f_toCPP(self, tree):
		头次创建 = engineLib.flexCAPI.f_创建物理形状几何体节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 头次创建 :
			self.use_custom_color = True
			self.color = bl_UI配色.g_物理节点颜色
			f_update_物理引擎类型(self, None)
			f_update_物理形状几何体类型(self, None)


class C_NODE_MENU_创建物理体形状(bpy.types.Operator):
	bl_idname = 'node.global_node_obj_add_physics_geometry'
	bl_label = '创建物理体形状'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_创建物理体形状', use_transform=True)
		return {'FINISHED'}













def f_update_物理体类型(self, context):
	data = list(str(self.m_物体物理类型))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	
	if context :
		self.id_data.f_节点数据更新_更新树([self])


class C_Node_创建物理体(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_创建物理体'
	bl_label = '创建物理体'
	
	m_物体物理类型 = bpy.props.EnumProperty(items=E_物理体类型枚举, name="属性", default="TYPE_刚体", update=f_update_物理体类型)
	def init(self, context):
		super(C_Node_创建物理体, self).f_init(context)
		self.inputs.new('C_Socket_I32', name='物理场景ID')
		self.inputs.new('C_Socket_物体_Array1D', name='物体')
		self.inputs.new('C_Socket_物理几何形状_Array1D', name='碰撞几何')
		self.inputs.new('C_Socket_物理材质_Array1D', name='物理材质')
		self.inputs.new('C_Socket_F32_Array1D', name='重量')
		
		self.outputs.new('C_Socket_物体_Array1D', name='物理体')
		
		self.f_toCPP(self.id_data);

	def draw_buttons(self, context, layout):
		
		layout.prop(self, 'm_物体物理类型')
		
		
	def f_toCPP(self, tree):
		头次创建 = engineLib.flexCAPI.f_创建物理体节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		#print('aaaaaaaaaaaaaaaaaaaaaaa')
		if 头次创建 :
			self.use_custom_color = True
			self.color = bl_UI配色.g_物理节点颜色
			f_update_物理体类型(self, None)
			


class C_NODE_MENU_创建物理体(bpy.types.Operator):
	bl_idname = 'node.global_node_obj_add_physics_body'
	bl_label = '创建物理体'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_创建物理体', use_transform=True)
		return {'FINISHED'}
















#self.report({'INFO'}, f"Number of evaluated vertices: {len(mesh_eval.vertices)}")













classes = (
	C_Node_创建物理材质,
	C_NODE_MENU_创建物理材质,

	C_Node_创建物理体,
	C_NODE_MENU_创建物理体,

	C_Node_创建物理体形状,
	C_NODE_MENU_创建物理体形状,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)








